package com.web.controller;

import com.github.pagehelper.PageInfo;
import com.web.entity.Consultation;
import com.web.entity.Result;
import com.web.service.ConsultationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/consultation")
@Slf4j
@CrossOrigin
public class ConsultationController {

    @Autowired
    private ConsultationService consultationService;

    //http:localhost:8080/consultation/list
    @GetMapping("/list")
    public Result page(Integer page,Integer pageSize,Consultation consultation){
        log.info("分页查询:{}",consultation);
        PageInfo<Consultation> list = this.consultationService.list(page,pageSize,consultation);
        return Result.success(list);
    }

    @GetMapping("/find")
    public Result findAll(){
        this.consultationService.findAll();
        return Result.success(this.consultationService.findAll());
    }

    @PostMapping("/send")
    public Result add(@RequestBody Consultation consultation){
        log.info("新增咨询:{}",consultation);
        this.consultationService.add(consultation);
        return Result.success(consultation);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(Integer id){
        this.consultationService.delete(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(Consultation consultation){
        this.consultationService.update(consultation);
        return Result.success();
    }

    @GetMapping("/findId")
    public Result findById(Integer id){
        Consultation consultation= this.consultationService.findById(id);
        return Result.success(consultation);
    }

    @GetMapping("/findUserId")
    public Result byUserId(Integer userId){
        List<Consultation> consultation = this.consultationService.byUserId(userId);
        return Result.success(consultation);
    }
}
